package com.quwan.manager.controller;

import com.quwan.common.BaseController;
import com.quwan.common.exception.ExpCode;
import com.quwan.common.result.ApiResult;
import com.quwan.common.result.PageResult;
import com.quwan.constans.WebConst;
import com.quwan.entity.TAdmin;
import com.quwan.entity.TMenu;
import com.quwan.manager.controller.dto.AdminDto;
import com.quwan.manager.service.AdminService;
import com.quwan.manager.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author laozhang
 * @date 2021/9/25 11:47
 */
@Api(tags = "后台管理员管理接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("mgr/admin")
public class AdminController extends BaseController {
    private final PasswordEncoder passwordEncoder;
    private final AdminService adminService;
    private final MenuService menuService;

    @ApiOperation(value = "管理员列表分页",notes = "<span style=\"color:red\">注意参数是否必填,返回数据中id——主键;loginName——登录名;name——名称;mobile——手机;status——状态(0正常 1关闭);remarks——备注;roleId——角色id;roleId——角色名称;addBy——增加人;addTime——增加时间;</span>",httpMethod = "POST")
    @PostMapping("adminListPage")
    public ApiResult<PageResult<AdminDto>> AdminListPage(@RequestParam(defaultValue = "1") int page,
                                                         @RequestParam(defaultValue = "10") int size,
                                                         @RequestParam(required = false) Long id,
                                                         @RequestParam(required = false) String name,
                                                         @RequestParam(required = false) String beginTime,
                                                         @RequestParam(required = false) String endTime) {
        PageResult<AdminDto> res = adminService.getAdminListPage(page, size, id, name, beginTime, endTime);
        return ApiResult.success(res);
    }

    @ApiOperation(value = "管理员列表",notes = "<span style=\"color:red\">注意参数是否必填,返回数据中id——主键;loginName——登录名;name——名称;mobile——手机;status——状态(0正常 1关闭);remarks——备注;roleId——角色id;roleId——角色名称;addBy——增加人;addTime——增加时间;</span>",httpMethod = "POST")
    @PostMapping("adminList")
    public ApiResult<List<AdminDto>> AdminList(@RequestParam(required = false) Long id,
                                             @RequestParam(required = false) String name,
                                             @RequestParam(required = false) String beginTime,
                                             @RequestParam(required = false) String endTime) {
        List<AdminDto> res = adminService.getAdminList(id, name, beginTime, endTime);
        return ApiResult.success(res);
    }

    @ApiOperation("管理员修改")
    @PostMapping("updateAdmin")
    public ApiResult updateAdmin(HttpServletRequest request,
                                 @ApiParam(value = "主键id")@RequestParam(required = false) Integer id,
                                 @ApiParam(value = "名称") @RequestParam(required = false)String name,
                                 @ApiParam(value = "昵称") @RequestParam(required = false)String nickName,
                                 @ApiParam(value = "角色Id") @RequestParam(required = false)Integer roleId,
                                 @ApiParam(value = "密码") @RequestParam(required = false)String password,
                                 @ApiParam(value = "备注") @RequestParam(required = false)String remarks){
        TAdmin admin = new TAdmin();
        admin.setId(Long.valueOf(id));
        admin.setLoginName(name);
        admin.setName(nickName);
        admin.setRoleId(roleId);
        admin.setRemarks(remarks);
        if (StringUtils.isNotBlank(password))
            admin.setPassword(passwordEncoder.encode(password));
        boolean success = adminService.updateAdmin(admin);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }

    @PostMapping("addAdmin")
    @ApiOperation("添加管理员")
    public ApiResult addAdmin(HttpServletRequest request,
                              @ApiParam(value = "账号")@RequestParam() String name,
                              @ApiParam(value = "昵称") @RequestParam(required = false)String nickName,
                              @ApiParam(value = "角色Id") @RequestParam(required = false)Integer roleId,
                              @ApiParam(value = "密码") @RequestParam(required = false)String password,
                              @ApiParam(value = "备注") @RequestParam(required = false)String remarks){
        TAdmin admin = new TAdmin();
        TokenInfo tokenInfo = getTokenInfo(request);
        admin.setAddBy(tokenInfo.getUsername());
        admin.setLoginName(name);
        admin.setName(nickName);
        admin.setRoleId(roleId);
        admin.setStatus(0);
        admin.setPassword(passwordEncoder.encode(password));
        admin.setRemarks(remarks);
        admin.setAddTime(LocalDateTime.now());
        boolean success = adminService.addAdmin(admin);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }

    @ApiOperation(value = "管理员详情",notes = "<span style=\"color:red\">注意参数是否必填,返回数据中id——主键;loginName——登录名;name——名称;mobile——手机;status——状态(0正常 1关闭);remarks——备注;roleId——角色id;roleId——角色名称;addBy——增加人;addTime——增加时间;</span>",httpMethod = "POST")
    @PostMapping("detailAdmin")
    public ApiResult detailAdmin(HttpServletRequest request){
        TokenInfo tokenInfo = getTokenInfo(request);
        Integer id = Integer.valueOf(tokenInfo.getId());
        TAdmin admin = adminService.detailAdmin(id);
        //admin.setPassword("");
        return ApiResult.success(admin);

    }

    @ApiOperation("登录成功——获取管理员菜单列表")
    @PostMapping("getAdminMenuList")
    public ApiResult getAdminMenuList(HttpServletRequest request,@ApiParam(value = "用户id")@RequestParam(required = true) Integer id){
        //TokenInfo tokenInfo = getTokenInfo(request);
        // id = Integer.valueOf(tokenInfo.getId());
        TAdmin admin = adminService.detailAdmin(id);
        List<TMenu> menuList = menuService.selectMenuListByRoleId(admin.getRoleId());
        return ApiResult.success(menuList);
    }

    @PostMapping("delAdmin")
    @ApiOperation("删除管理员")
    public ApiResult delAdmin(@RequestParam(value = "id") Integer id){
        if(id.equals(1)){
            return ApiResult.error(ExpCode.SYSTEM_NOT_POWER.getMsg());
        }
        boolean success = adminService.delAdmin(id);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }

    @PostMapping("updatePassword")
    @ApiOperation("修改密码")
    public ApiResult updatePassword(HttpServletRequest request,
                              @ApiParam(value = "主键id")@RequestParam(required = true) Integer id,
                              @ApiParam(value = "旧密码") @RequestParam(required = true)String oldPwd,
                              @ApiParam(value = "新密码") @RequestParam(required = true)String newPwd,
                              @ApiParam(value = "确认密码") @RequestParam(required = true)String conPwd){
        if(!newPwd.equals(conPwd)){
            return ApiResult.error(ExpCode.PASSWORD_NOT_SAME.getMsg());
        }
        TAdmin admin = adminService.detailAdmin(id);
        boolean b = passwordEncoder.matches(oldPwd,admin.getPassword());
        if(!b){
            return ApiResult.error(ExpCode.PASSWORD_NOT_OLD.getMsg());
        }
        TAdmin ad = new TAdmin();
        ad.setId(admin.getId());
        ad.setPassword(passwordEncoder.encode(newPwd));
        boolean success = adminService.updateAdmin(ad);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }

    @ApiOperation("开启/禁用 管理员")
    @PostMapping("updateStatus")
    public ApiResult updateStatus(@ApiParam("id") @RequestParam Integer id,
                               @ApiParam("状态0正常 1关闭") @RequestParam Integer status) {

        TAdmin admin = new TAdmin();
        admin.setId(Long.valueOf(id));
        admin.setStatus(status);
        boolean success = adminService.updateAdmin(admin);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();

    }

}
